﻿using Deployment.Data.UI.Controls.Deploy;
using Deployment.Data.UI.Dialogs.Task;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Task
{
    /// <summary>
    /// Interaction logic for EditableTaskList.xaml
    /// </summary>
    public partial class EditableTaskList : UserControl
    {
        public static readonly DependencyProperty CurrentStepProperty =
            DependencyProperty.Register("CurrentStep",
                typeof(Data.Deploy.Steps.Step),
                typeof(EditableTaskList));

        public static readonly DependencyProperty EnvironmentsProperty =
            DependencyProperty.Register("Environments",
                typeof(Data.Environment.EnvironmentsFile),
                typeof(EditableTaskList));



        public Data.Environment.EnvironmentsFile Environments
        {
            get { return (Data.Environment.EnvironmentsFile)GetValue(EnvironmentsProperty); }
            set { SetValueDp(EnvironmentsProperty, value); }
        }

        public EditableTaskList()
        {
            InitializeComponent();
        }

        public event DependencyPropertyChangedEventHandler PropertyChanged;

        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public Data.Deploy.Steps.Step CurrentStep
        {
            get { return (Data.Deploy.Steps.Step)GetValue(CurrentStepProperty); }
            set { SetValueDp(CurrentStepProperty, value); }
        }

        private void AddTask_Click(object sender, RoutedEventArgs e)
        {
            DeployEditorControl parent = WPFHelpers.TryFindParent<DeployEditorControl>(this);
            TaskDialog dlg = new TaskDialog();
            dlg.Owner = Window.GetWindow(this);
            dlg.CurrentStep = CurrentStep;
            dlg.Environments = Environments;
            bool? result = dlg.ShowDialog();
            if (result.HasValue && result.Value == true)
            {
                this.CurrentStep.AddTask(dlg.NewTask);
                if (parent != null)
                    parent.HasUnsavedChanges = true;
            }
        }

        private void RemoveTask_Click(object sender, RoutedEventArgs e)
        {
            //TODO
            MessageBox.Show("Not yet Implemented.");
        }
    }
}
